<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="问题流水单号" prop="checkProblemNum">
        <el-input
          v-model="queryParams.checkProblemNum"
          placeholder="请输入问题流水单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题类型" prop="problenType">
        <DictRadio v-model="queryParams.problenType" :radioData="problenTypeOption" :showAll="'all'"
                   size="small" @change="handleQuery"></DictRadio>
      </el-form-item>

      <el-form-item label="问题状态" prop="problemStatus">
        <el-select v-model="queryParams.problemStatus" placeholder="请选择问题状态">
          <el-option
            v-for="item in problemStatusOption"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['check:checkProblem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['check:checkProblem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['check:checkProblem:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['check:checkProblem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkProblemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="施工检查问题id" align="center" prop="checkProblemId" />
      <el-table-column label="项目报备单号" align="center" prop="reportNum" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="问题流水单号" align="center" prop="checkProblemNum" >
        <template slot-scope="scope">
          <router-link :to="'/check/checkProblem/detail/'+scope.row.checkProblemId" class="link-type">
            <span>{{ scope.row.checkProblemNum }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="problenType">
        <template slot-scope="scope">
          <span v-if="scope.row.problenType == 0">施工问题</span>
          <span v-if="scope.row.problenType == 1">撤场问题</span>
        </template>
      </el-table-column>
      <el-table-column label="问题内容" align="center" prop="problemContent" :show-overflow-tooltip='true'/>
      <el-table-column label="负责人" align="center" prop="headName" />
      <el-table-column label="检查人" align="center" prop="checkByName" />
      <el-table-column label="检查时间" align="center" prop="checkTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="归属管辖大队" align="center" prop="deptName" />
      <el-table-column label="问题状态" align="center" prop="problemStatus" width="160">
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == null" effect="plain" size="medium" type="info">未提交整改单</el-tag>
          <el-tag  v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == 0" effect="plain" size="medium" type="success">未提交整改单-通过</el-tag>
          <el-tag  v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == 1" effect="plain" size="medium" type="danger">未提交整改单-不通过</el-tag>
          <el-tag  v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == null" effect="plain" size="medium" type="warning">已提交整改单-审核中</el-tag>
          <el-tag  v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == 0" effect="plain" size="medium" type="success">已提交整改单-通过</el-tag>
          <el-tag  v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == 1" effect="plain" size="medium" type="danger">已提交整改单-不通过</el-tag>

<!--          <span v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == null" style="color: #FFB009">未提交整改单</span>-->
<!--          <span v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == 0" style="color: #59DD34">未提交整改单-通过</span>-->
<!--          <span v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == 1" style="color: #FF0000">未提交整改单-不通过</span>-->

<!--          <span v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == null" style="color: #FFB009">已提交整改单-审核中</span>-->
<!--          <span v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == 0" style="color: #59DD34">已提交整改单-通过</span>-->
<!--          <span v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == 1" style="color: #FF0000">已提交整改单-不通过</span>-->
<!--          <span v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == null" >未整改-<img  src="@/assets/customer/dsh2.svg"></span>
          <span v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == 0" >未整改-<img  src="@/assets/customer/tg.svg"></span>
          <span v-if="scope.row.problemStatus == 0 && scope.row.approvalStatus == 1" >未整改-<img src="@/assets/customer/btg2.svg"></span>

          <span v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == null" >已整改-<img  src="@/assets/customer/dsh2.svg"></span>
          <span v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == 0" >已整改-<img   src="@/assets/customer/tg.svg"></span>
          <span v-if="scope.row.problemStatus == 1 && scope.row.approvalStatus == 1" >已整改-<img src="@/assets/customer/btg2.svg"></span>-->
        </template>
      </el-table-column>
      <el-table-column label="整改时间" align="center" prop="rectificationTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rectificationTime,'{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['check:checkProblem:edit']"
            v-show="scope.row.problemStatus ==0 && scope.row.approvalStatus == null && scope.row.head == currentUserId"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['check:checkProblem:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改施工检查问题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-divider content-position="center">检查信息</el-divider>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目报备单号：">{{ form.reportNum }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="问题流水单号：">{{ form.checkProblemNum }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="问题类型：">{{ form.problenType == 0 ? '施工问题' :'撤场问题' }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="问题内容：">{{ form.problemContent }}</el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="问题照片/视频">
            <image-preview  :value="this.problemFileData"/>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="经纬度：">{{ form.checkLongitude + ',' + form.checkLatitude }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="道路名称：">{{ form.checkRoadName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="方向：">{{ form.checkDirection }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="所属管辖大队：">{{ form.deptName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检查人员：">{{ form.checkByName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检查时间：">{{ parseTime(form.checkTime) }}</el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="center">整改信息</el-divider>
        <el-row>
          <el-form-item label="整改描述：" prop="rectificationRemark">
            <el-input v-model="form.rectificationRemark" type="textarea" placeholder="请输入整改描述" />
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="整改照片/视频" prop="rectificationFileId">
            <file-upload  @input="getFileList" :fileLocationName="'rectificationFile'" :fileType="['png','jpg','jpeg','video','mp4']" :limit="5" :fileSize="50"/>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="负责人：">{{ form.headName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="整改时间" prop="rectificationTime">
              <el-date-picker clearable
                              v-model="form.rectificationTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              style="width: 200px"
                              placeholder="请选择整改时间"
                              :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 查看施工检查问题对话框 -->
    <el-dialog :title="title" :visible.sync="viewOpen" width="900px" append-to-body>
      <el-divider content-position="center">检查信息</el-divider>
      <el-form label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目报备单号：">{{ form.reportNum }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="问题流水单号：">{{ form.checkProblemNum }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="问题类型：">{{form.problenType == 0 ? '施工问题' :'撤场问题' }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="问题内容：">{{ form.problemContent }}</el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="问题照片/视频" prop="problemFileId">
            <image-preview  :value="this.problemFileData"/>
          </el-form-item>
        </el-row>

        <el-row>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="经纬度：">{{ form.checkLongitude + ',' + form.checkLatitude }}</el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="道路名称：">{{ form.checkRoadName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方向：">{{ form.checkDirection }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="所属管辖大队：">{{ form.deptName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检查人员：">{{ form.checkByName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检查时间：">{{ parseTime(form.checkTime) }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider content-position="center">整改上报信息</el-divider>
      <el-form label-width="120px" v-show="form.rectificationReportId != null">
        <el-row>
          <el-form-item label="整改描述：">{{ form.rectificationRemark }}</el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="整改照片/视频" prop="rectificationFileId">
            <image-preview :value="this.rectificationFileData"/>
          </el-form-item>
        </el-row>
        <el-row>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="经纬度：">{{ form.rectificationLongitude + ',' + form.rectificationLatitude}}</el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="道路名称：">{{ form.rectificationRoadName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方向：">{{ form.rectificationDirection }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="整改人：">{{ form.headName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="整改时间：">{{ parseTime(form.rectificationTime,'{y}-{m}-{d}') }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider content-position="center">整改复查信息</el-divider>
      <el-form label-width="120px" v-show="form.rectificationReviewId != null">
        <el-row>
          <el-form-item label="复查意见：">{{ form.reviewContent }}</el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="复查照片/视频" >
            <image-preview :value="this.reviewFileData"/>
          </el-form-item>
        </el-row>
        <el-row>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="经纬度：">{{ form.reviewLongitude + ',' + form.reviewLatitude}}</el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="道路名称：">{{ form.reviewRoadName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方向：">{{ form.reviewDirection }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="复查人：">{{ form.reviewByName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="复查时间：">{{ parseTime(form.reviewTime,'{y}-{m}-{d}') }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckProblem, getCheckProblem, delCheckProblem, addCheckProblem, updateCheckProblem } from "@/api/check/checkProblem";

export default {
  name: "CheckProblem",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 施工检查问题表格数据
      checkProblemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkProblemNum: null,
        problenType: null,
        problemContent: null,
        head: null,
        problemStatus: null,
      },
      // 表单参数
      form: {},
      //  现场问题及视频 附件
      problemFileData:[],

      //  是否显示详情弹出层
      viewOpen: false,

      //  整改问题照片及视频
      rectificationFileData:[],

      //  整改复查照片及视频
      reviewFileData:[],

      //  问题类型
      problenTypeOption:[{label:'施工问题',value:0},{label:'撤场问题',value:1}],

      //  问题状态
      problemStatusOption:[{label:'未提交整改单',value:0},{label:'已提交整改单',value:1}],

      //当前登录人
      currentUserId: this.$store.state.user.id,
      // 表单校验
      rules: {
        checkRecordId: [
          { required: true, message: "施工检查记录id不能为空", trigger: "blur" }
        ],
        checkProblemNum: [
          { required: true, message: "问题流水单号不能为空", trigger: "blur" }
        ],
        rectificationRemark: [
          { required: true, message: "整改描述不能为空", trigger: "blur" }
        ],
        head: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
      },
      pickerOptions: {
        disabledDate: (time) => {
          let nowData = new Date()
          nowData = new Date(nowData.setDate(nowData.getDate() - 1))
          return time < nowData
        }
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询施工检查问题列表 */
    getList() {
      this.loading = true;
      listCheckProblem(this.queryParams).then(response => {
        this.checkProblemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkProblemId: null,
        checkRecordId: null,
        checkProblemNum: null,
        problenType: null,
        problemContent: null,
        head: null,
        problemStatus: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      this.problemFileData = [];
      this.rectificationFileData = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkProblemId)
      // this.single = selection.length!==1
      //控制修改按钮  只有状态等于 0
      this.single = !(selection.length===1 && (selection[0].problemStatus === 0 && selection[0].approvalStatus === null && selection[0].head === this.currentUserId));
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加施工检查问题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const checkProblemId = row.checkProblemId || this.ids
      getCheckProblem(checkProblemId).then(response => {
        const { code, data } = response;
        this.form = data;
        //  问题图片/视频
        this.problemFileData =data.problemFile;
        this.open = true;
        this.title = "修改施工检查问题";
        //  设置负责人
        this.form.headName = this.$store.state.user.nickName;
        //日期区间回显
        this.$set(this.form, 'rectificationTime', new Date())
      });
    },

    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const checkProblemId = row.checkProblemId || this.ids
      getCheckProblem(checkProblemId).then(response => {
        const { code, data } = response;
        this.form = data;
        //  问题图片/视频
        this.problemFileData = data.problemFile;
        //  整改图片及视频
        this.rectificationFileData = data.rectificationFile;
        //复查图片及视频
        this.reviewFileData = data.reviewFile

        this.viewOpen = true;
        this.title = "查看施工检查问题";
        //  日期区间回显
        // this.$set(this.form, 'rectificationTime', new Date())
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkProblemId != null) {
            updateCheckProblem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckProblem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkProblemIds = row.checkProblemId || this.ids;
      this.$modal.confirm('是否确认删除施工检查问题编号为"' + checkProblemIds + '"的数据项？').then(function() {
        return delCheckProblem(checkProblemIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('check/checkProblem/export', {
        ...this.queryParams
      }, `checkProblem_${new Date().getTime()}.xlsx`)
    },

    //获取附件列表
    getFileList(fileLocationName,fileIds,deleteFileId){
      this.form[fileLocationName] = fileIds;
    },
  }
};
</script>
